﻿using IDAL;
using Model;
using NHibernate;
using System.Collections.Generic;

namespace SqlDALLibrary
{
    public class UserRelationDal:IUserRelationDAL
    {
        private static EntityControl control;
        static UserRelationDal()
        {
            control = EntityControl.CreateEntityControl("Model");
        }
        public void saveUserRelation(UserRelation relation)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Save(relation);
                tx.Commit();
            }
            session.Close();
        }

        public void updateUserRelation(UserRelation relation)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Update(relation);
                tx.Commit();
            }
            session.Close();
        }

        public void delateUserRelation(UserRelation relation)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Delete(relation);
                tx.Commit();
            }
            session.Close();
        }

        public UserRelation getUserRelation(int id)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            UserRelation relation = session
                .CreateQuery("from UserRelation n where n.id=:id")
                .SetInt32("id", id)
                .UniqueResult<UserRelation>();
            session.Close();
            return relation;
        }

        public IList<UserRelation> getUserRelation(User user)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<UserRelation> userRelationList = session
                .CreateQuery("from UserRelation n where n.who=:who")
                .SetParameter("who", user)
                .List<UserRelation>();
            session.Close();
            return userRelationList;
        }

        public IList<UserRelation> getUserRelationFollow(User user)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<UserRelation> userRelationList = session
                .CreateQuery("from UserRelation n where n.follow=:follow")
                .SetParameter("follow", user)
                .List<UserRelation>();
            session.Close();
            return userRelationList;
        }

        public IList<UserRelation> getUserRelation()
        {
            return control.getSession()
                .CreateQuery("from UserRelation")
                .List<UserRelation>();
        }

        public UserRelation getUserRelation(User who, User follow)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            UserRelation userRelation = session
                .CreateQuery("from UserRelation n where n.who=:who and n.follow=:follow")
                .SetParameter("who", who)
                .SetParameter("follow", follow)
                .UniqueResult<UserRelation>();
            session.Close();
            return userRelation;
        }

    }
}
